﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace DrivingCRM.Views.Private.CRM
{
    public partial class ManageMotorcycles : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] != null || (Session["User"] as List<string>)[0] == "Client")
            {
                /// si c'est un vendeur ou  un intervenant il n'a accès qu'au véhicule de son agence
                if ((Session["User"] as List<string>)[0] == "Salesman" || (Session["User"] as List<string>)[0] == "Stakeholder")
                {
                    this.DS_Agencies.Where = "it.IdAgency == @IdAgency";
                    this.DS_Agencies.WhereParameters.Add(new Parameter() { DbType = System.Data.DbType.Int32, Name = "IdAgency", DefaultValue = (Session["User"] as List<string>)[2] });
                    this.DDL_Agencies.Enabled = false;
                }
                else
                    this.DDL_Agencies.Enabled = true;
            }
            else
            {
                Response.Redirect("~/Views/Default.aspx");
            }
        }
        protected void B_AddMotorcycle_Click(object sender, EventArgs e)
        {
            /// affichage du template d'insertion d'un package en première position de la listView
            LV_Motorcyles.InsertItemPosition = InsertItemPosition.FirstItem;
        }
        protected void LV_Motorcyles_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            /// si l'annulation a été déclenchée pendant l'insertion
            if (e.CancelMode == ListViewCancelMode.CancelingInsert)
            {
                /// le formulaire d'ajout est alors caché
                LV_Motorcyles.InsertItemPosition = InsertItemPosition.None;
            }
        }

        protected void LV_Motorcyles_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            /// ajout de la valeur de la combobox de l'agence à la voiture en cours d'insertion 
            e.Values["AgencyIdAgency"] = int.Parse(this.DDL_Agencies.SelectedValue);

            /// gestion des datepicker
            e.Values["Created_year"] = ((RadDatePicker)LV_Motorcyles.InsertItem.FindControl("RDP_CreatedYear")).SelectedDate.Value;
            e.Values["LastRevisionDate"] = ((RadDatePicker)LV_Motorcyles.InsertItem.FindControl("RDP_LastRevisionDate")).SelectedDate.Value;
        }

        protected void LV_Motorcyles_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            /// ajout de la valeur de la combobox de l'agence à la voiture en cours modification 
            e.NewValues["AgencyIdAgency"] = int.Parse(this.DDL_Agencies.SelectedValue);

            /// gestion des datepicker
            e.NewValues["Created_year"] = ((RadDatePicker)LV_Motorcyles.EditItem.FindControl("RDP_CreatedYear")).SelectedDate.Value;
            e.NewValues["LastRevisionDate"] = ((RadDatePicker)LV_Motorcyles.EditItem.FindControl("RDP_LastRevisionDate")).SelectedDate.Value;
        }
    }
}